<template>
  <view>
    <view class="goods-list" >
      <view v-for="goods in goodsList" :key="goods.cat_id" @click="gotoDetail(goods)">
        <mygoods :goods="goods"></mygoods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:'',
       isLoading:false
      };
    },
    onLoad(options) {
      this.queryObj.query=options.query||''
      this.queryObj.cid=options.cid||''
      this.getgoodsList()
    },
    methods:{
      async getgoodsList(cb){
        this.isLoading=true
        const {data:res} =await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isLoading=false
          cb&&cb()
        if(res.meta.status!==200) return $showMsg()
        this.goodsList=[...this.goodsList,...res.message.goods]
        this.total=res.message.total
      },
      gotoDetail(good) {
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+good.goods_id
        })
      }
    },
    onReachBottom(){
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据加载完成')
      if(this.isLoading) return 
      this.queryObj.pagenum+=1
      this.getgoodsList()
    },
    onPullDownRefresh(){
      this.goodsList=[]
      this.queryObj.pagenum=1
      this.total=0
      this.isLoading=false
      this.getgoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
